/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.workflow.service.impl;

import me.zhengjie.modules.workflow.domain.ProcessCategory;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.workflow.repository.ProcessCategoryRepository;
import me.zhengjie.modules.workflow.service.ProcessCategoryService;
import me.zhengjie.modules.workflow.service.dto.ProcessCategoryDto;
import me.zhengjie.modules.workflow.service.dto.ProcessCategoryQueryCriteria;
import me.zhengjie.modules.workflow.service.mapstruct.ProcessCategoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author wzp
* @date 2020-10-28
**/
@Service
@RequiredArgsConstructor
public class ProcessCategoryServiceImpl implements ProcessCategoryService {

    private final ProcessCategoryRepository processCategoryRepository;
    private final ProcessCategoryMapper processCategoryMapper;

    @Override
    public Map<String,Object> queryAll(ProcessCategoryQueryCriteria criteria, Pageable pageable){
        Page<ProcessCategory> page = processCategoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(processCategoryMapper::toDto));
    }

    @Override
    public List<ProcessCategoryDto> queryAll(ProcessCategoryQueryCriteria criteria){
        return processCategoryMapper.toDto(processCategoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ProcessCategoryDto findById(Long categoryId) {
        ProcessCategory processCategory = processCategoryRepository.findById(categoryId).orElseGet(ProcessCategory::new);
        ValidationUtil.isNull(processCategory.getCategoryId(),"ProcessCategory","categoryId",categoryId);
        return processCategoryMapper.toDto(processCategory);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ProcessCategoryDto create(ProcessCategory resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setCategoryId(snowflake.nextId()); 
        return processCategoryMapper.toDto(processCategoryRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ProcessCategory resources) {
        ProcessCategory processCategory = processCategoryRepository.findById(resources.getCategoryId()).orElseGet(ProcessCategory::new);
        ValidationUtil.isNull( processCategory.getCategoryId(),"ProcessCategory","id",resources.getCategoryId());
        processCategory.copy(resources);
        processCategoryRepository.save(processCategory);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long categoryId : ids) {
            processCategoryRepository.deleteById(categoryId);
        }
    }

    @Override
    public void download(List<ProcessCategoryDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ProcessCategoryDto processCategory : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("分类名称", processCategory.getCategoryName());
            map.put("排序", processCategory.getCategorySort());
            map.put("状态", processCategory.getEnabled());
            map.put("创建者", processCategory.getCreateBy());
            map.put("更新者", processCategory.getUpdateBy());
            map.put("创建日期", processCategory.getCreateTime());
            map.put("更新时间", processCategory.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}